<template>
  <div class="playList-offical">
    <h1>官方歌单</h1>
    <div class="wrapper" ref="wrapper">
      <ul class="content" ref="content">
        <li v-for="item in playListData" :key="item.content_id"
         @click="selectPlayList(item)">
            <div class="cover">
              <img :src="item.cover" alt=""/>
              <span>{{ item.listen_num | numFilter(2)}}万</span>
            </div>
              <span>{{ item.title }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import BScroll from 'better-scroll';
export default {
  data() {
    return {
      playListData: [],
    };
  },
  created() {
    this.getPlayList();
  },
  methods: {
    getPlayList() {
      axios
        .get("/api/recommend/playlist/u")
        .then((res) => {
          console.log(res.data);
          this.playListData = res.data.data.list;
          //得到异步数据之后调用水平滚动的方法
          this.betterScrollHorizonal(this.playListData.length,160);
        })
        .catch((err) => {
          console.error(err);
        });
    },
    //封装水平滚动的方法
    betterScrollHorizonal(num,itemWidth){
      
      let width = num * itemWidth;
      this.$refs.content.style.width = width + "px";//设置content的宽度
      this.$nextTick(() => {
        this.scroll = new BScroll(this.$refs.wrapper,{
          scrollX: true,//横向滚动打开
          scrollY: false,//纵向滚动关闭
          startX: 0,
          click: true,
        })
      })
    },
    selectPlayList(item){
      const id = item.content_id
      // this.$router.push({
      //   name:"PlayListDetail",
      //   params:{id}
      // })
      this.$router.push({
          path:`/PlayListDetail/${id}`
      })
    }
  },
  filters:{
    numFilter(value,num){
      return parseFloat(value / 10000).toFixed(num);
    }
  }
};
</script>
<style scoped>
.playList-offical{
  width: 90%;
  margin: 0 auto;
}

.playList-offical > h1{
  font-size: 18px;
  text-align: left;
}

.wrapper{
  width: 100%;
  overflow: hidden;
}

ul.content > li {
  width: 160px;
  display: inline-block;
  text-align: center;
  vertical-align: top;
}

ul.content > li img {
  width: 90%;
  border-radius: 20px;

}

ul.content > li >span {
  display: block;
  width: 90%;
  margin:  0 auto;
  font-size: 12px;
}

.cover {
  position: relative;
}

.cover >span{
  display: block;
  position: absolute;
  right: 10px;
  bottom: 5px;
  z-index: 100;
  background-color: gray;
  border-radius: 20px;
  width: 100px;
  
}
</style>